﻿#region GNU GPL Version 3 License

/// Copyright 2013 Shailesh Lolam
/// 
/// This file CloudService.cs is part of AzEzAuto.
/// 
/// AzEzAuto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
/// 
/// AzEzAuto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/// 
/// You should have received a copy of the GNU General Public License along with AzEzAuto. If not, see http://www.gnu.org/licenses/.
/// 
/// History:
/// ______________________________________________________________
/// Created         04-2013             Shailesh Lolam

#endregion
      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XecMe.Core.Tasks;
using AzEzAuto.Entities.Requests;


namespace AzEzAuto.Orchestrate.Tasks
{
    public class CloudService : BaseTask
    {
        protected override ExecutionState Process(Request req)
        {
            
            ///If there is no Hosted Service part in the request, just move it to next guy in the pipeline
            if (req.CloudService == null)
            {
                WriteToOutputQueue(req);
                return ExecutionState.Executed;
            }

            if (string.IsNullOrEmpty(req.CloudService.ServiceName))
            {
                req.Error = new Error() { RequestId = "-1", Code = "-1", Message = "Service name is missing for the Hosted Service request " + req.SubscriptionId };
                WriteToErrorQueue(req);
            }

            switch (req.CloudService.Operation)
            {
                case CloudServiceBase.OperationType.Create:
                    {
                        CreateCloudService chs = (CreateCloudService)req.CloudService;
                        Entities.Hosted.CreateHostedService r = new Entities.Hosted.CreateHostedService()
                        {
                            Description = chs.Description,
                            Label = chs.Label,
                            ServiceName = chs.ServiceName,
                            AffinityGroup = chs.AffinityGroup,
                            Location = chs.Location
                        };
                        CopyProperties(chs.Properties, r.ExtendedProperties);
                        req.CurrentRequestId = _api.Cloud.CreateCloudService(req.SubscriptionId, r);
                        break;
                    }
                case CloudServiceBase.OperationType.Update:
                    {
                        UpdateCloudService uhs = (UpdateCloudService)req.CloudService;
                        Entities.Hosted.UpdateHostedService r = new Entities.Hosted.UpdateHostedService()
                        {
                            Description = uhs.Description,
                            Label = uhs.Label,
                        };
                        CopyProperties(uhs.Properties, r.ExtendedProperties);
                        req.CurrentRequestId = _api.Cloud.UpdateCloudService(req.SubscriptionId, r, uhs.ServiceName);
                    }
                    break;
                case CloudServiceBase.OperationType.Delete:
                    req.CurrentRequestId = _api.Cloud.DeleteCloudService(req.SubscriptionId, req.CloudService.ServiceName);
                    break;
                default:
                    throw new InvalidOperationException(string.Format("Unknown operation {0} for the Hosted Service for subscription {1}", req.CloudService.Operation, req.SubscriptionId));
            }

            ///Monitor the operation until completed
            RewriteToInputQueue(req);
            return ExecutionState.Executed;
        }
    }
}
